package com.seewintech.framework.common.http;

import com.seewintech.framework.common.exception.ExceptionEnum;
import com.seewintech.framework.common.exception.ServiceException;
import com.seewintech.framework.common.success.AjaxResult;
import com.seewintech.framework.common.success.DefaultSuccessEnum;
import com.seewintech.framework.common.success.SuccessEnum;
import lombok.Data;
import org.springframework.http.HttpStatus;


/**
 * @author by xiaoTian
 * @date 2022/6/14.
 */
@Data
public class Result<T>  {

    /**
     * 返回码
     */
    private int code;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;


    public interface Body {
        Integer SUCCESS_CODE = 0;
    }

    protected Result(int code, T data, String message) {
        this.code =code;
        this.message = message;
        this.data = data;
    }


    /*******************************成功********************************************/
    public static Result<String> success() {
        return Result.success(DefaultSuccessEnum.DEFAULT_SUCCESS);
    }

    public static Result<String> success(SuccessEnum successEnum) {
        return Result.success(successEnum.getString());
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(Body.SUCCESS_CODE, data, HttpStatus.OK.getReasonPhrase());
    }

    public static <T> Result<T> ajaxResult(AjaxResult ajaxResult){
        return new Result<>(ajaxResult.getCode(),null,ajaxResult.getMessage());
    }

    /*******************************失败********************************************/
    public static <T> Result<T> error(ServiceException e) {
        return Result.error(e.getCode(), e.getMessage());
    }

    public static <T> Result<T> error(ExceptionEnum e) {
        return Result.error(e.getCode(), e.getMessage());
    }


    public static <T> Result<T> error(int code, String message) {
        return Result.error(code, message);
    }

}
